<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>鼓励金红包</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> 
		<link href="__INDEX__/css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="__INDEX__/store/css/app.css" />
		<link rel="stylesheet" href="__INDEX__/store/css/style.css" />
		<link rel="stylesheet" href="__INDEX__/store/css/newsj.css" />
	</head>
    <style>
    	.mui-switch.mui-active:before,.mui-switch:before{
    		top: 3px;
    	}
    	.mui-content{
    		background: white;
    	}
    </style>
	<body>
		<header class="mui-bar mui-bar-nav hb_bar_nav">
			<a class="mui-icon mui-icon-left-nav mui-pull-left" href="JavaScript:history.go(-1)"></a>
			<h1 class="mui-title">鼓励金红包</h1>
		</header>
		<div class="mui-content neirong">
			<input type="hidden" id="page" value="1" />
	 
			 
			<!--<li class="clearfix">
				<style>
					.sjindexcon {
						background: #F2F2F2;
					}
				</style>
				<div style="width:100%;text-align: center;margin-top: 120px;"><img src="__INDEX__/store/images/bac_hbjl.jpg" /></div>
			</li>-->
		 

		</div>
		<div class="hbjj_db">
			<a href="hongb_pin.html"><img src="__INDEX__/store/images/icon_fhb.png"></a>
		</div>
		<div class="mask" style="display: none;"></div>
		<div class="tanchubox" style="display: none;">
			<input type="hidden" name="id" id="hb_id">
			<div class="tanc_item">
				<div class="tips">追加人数</div>
				<div class="tanc_iinput"><input type="text" placeholder="请输入追加人数" name="user_num" id="user_num" required lay-verify="required" autocomplete="off" class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')"></div>
			</div>
			<div class="tanc_item">
				<div class="tips">增加鼓励金</div>
				<div class="tanc_iinput"><input type="text" placeholder="请输入增加的鼓励金" name="gold_sum" id="gold_sum" onkeyup="keyup(this)" required lay-verify="required" autocomplete="off" class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')"></div>
			</div>
			<div class="tanc_item">
				<div class="tips">备注</div>
				<div class="tanc_iinput"><input type="text" placeholder="鼓励金可在附近商家消费抵现" name="memo" id="memo" required autocomplete="off" class="layui-input"></div>
			</div>
			<button type="button" class="tanc_btn">确定</button>
			<div class="close" onclick="$('.tanchubox').hide()"><span class="mui-icon mui-icon-closeempty"></span></div>
		</div>
	</body>

</html>

{include file="public/footer" /}
<script type="text/javascript" src="__INDEX__/js/menudown.js"></script>
<script>
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	mui('.mui-content .mui-switch').each(function() { //循环所有toggle
		//toggle.classList.contains('mui-active') 可识别该toggle的开关状态
		this.parentNode.querySelector('span').innerText = '状态：' + (this.classList.contains('mui-active') ? 'true' : 'false');
		/**
		 * toggle 事件监听
		 */
		this.addEventListener('toggle', function(event) {
			//event.detail.isActive 可直接获取当前状态
			this.parentNode.querySelector('span').innerText = '状态：' + (event.detail.isActive ? 'true' : 'false');
		});
	});
</script>
<script> 
    gethb();
    function gethb(){
    	$.ajax({
			url: "{:url('agent/hb_list')}",
			data: { 
				page:$("#page").val()
			},
			success: function(data) { 
				var list=data.list;
				var html="";
				var leix = '';
				var p = ''; 
				var yangshi = '';
				if(list != ''){
					for(var i=0;i<list.length;i++){
						var log=list[i];  
						if(log.status == 1){
							yangshi = 'mui-active';
							type = 1;
						}else{
							yangshi = '';
							type = 2;
						}
						if(log.hb_type == 2){
							p = '拼红包';
							}else{
							p = '普通红包';
						}
							
						if(log.type == 1){
							leix = '(锁粉)';
							}else if(log.type == 2){
								leix = '(分享)';
							}else{
								leix = '(扫码)';
						}
						html+='<div class="hbjl_item clearfix">'
						    +'<div class="top clearfix">'
						    +'<div class="left">'
						    +'<a style="color: #999999;" href="hongb_logo.html?m='+log.id+'">'+p+'<em style="margin-left: 5px">'+leix+'</em></a>'
						    +"</div>"
						    +'<div class="right" onclick="hb('+log.id+','+type+')">'
						    +'<div class="mui-switch '+yangshi+'">'
						    +'<div class="mui-switch-handle"></div>'
						    +"</div>"
						    +"</div>"
						    +"</div>" 
						    +'<a href="hongb02.html?m='+log.id+'">'
						    +'<div class="hbjl_hb clearfix">'
						    +'<div class="left">¥<em>'+log.gold_sum+'</em></div>'
						    +'<div class="right">'
						    +'<p>'+log.user_num+'个/'+log.peoples+'人已领取</p>'
						    +'<p class="time">'+log.add_time+'</p>'
						    +"</div>"
						    +"</div>"
						    +"</a>"  
						    +'<button type="button" class="hbjj_btn" onclick="gold_zj('+log.id+')">追加</button>' 
						    +"</div>"
					}
				}else{ 
					$('.mui-content').css('background','#efeff4');
					html='<div style="margin-top:100px"><img src="__INDEX__/store/images/bac_hbjl.jpg" /></div>';
				}
				$(".neirong").html(html);
			}

		})
    }
    
    $(window).scroll(function() {
		var scrollTop = $(window).scrollTop(); //滚动条高度
		var bodyheight = $(document).height(); //文档的高度
		var windowheight = window.innerHeight; //$(window).height();              //当前窗体的高度
		if((bodyheight - windowheight - scrollTop) == 0) {
			$.ajax({
				type: "post",
				data: {
					page: parseFloat($("#page").val()) + 1
				},
				url: "{:url('agent/hb_list')}",
				success: function(data) {
					var list = data.list;
					var html = "";
					var p = '';
					var leix = '';
					var yangshi = '';
					if(list) {
						$("#page").val(parseFloat($("#page").val()) + 1);
                        for(var i=0;i<list.length;i++){
							var log=list[i];  
							if(log.status == 1){
								yangshi = 'mui-active';
								type = 1;
							}else{
								yangshi = '';
								type = 2;
							}
							if(log.hb_type == 2){
								p = '拼红包';
							}else{
								p = '普通红包';
							}
							
						    if(log.type == 1){
								leix = '(锁粉)';
							}else if(log.type == 2){
								leix = '(分享)';
							}else{
								leix = '(扫码)';
							}
							html+='<div class="hbjl_item clearfix">'
							    +'<div class="top clearfix">'
							    +'<div class="left">'
							    +'<a style="color: #999999;" href="hongb_logo.html?m='+log.id+'">'+p+'<em style="margin-left: 5px">'+leix+'</a>'
							    +"</div>"
							    +'<div class="right" onclick="hb('+log.id+','+type+')">'
							    +'<div class="mui-switch '+yangshi+'">'
							    +'<div class="mui-switch-handle"></div>'
							    +"</div>"
							    +"</div>"
							    +"</div>" 
							    +'<a href="hongb02.html?m='+log.id+'">'
							    +'<div class="hbjl_hb clearfix">'
							    +'<div class="left">¥<em>'+log.gold_sum+'</em></div>'
							    +'<div class="right">'
							    +'<p>'+log.user_num+'个/'+log.peoples+'人已领取</p>'
							    +'<p class="time">'+log.add_time+'</p>'
							    +"</div>"
							    +"</div>"
							    +"</a>"  
							    +'<button type="button" class="hbjj_btn" onclick="gold_zj('+log.id+')">追加</button>' 
							    +"</div>"
					}
					} else {
						html = "已经到最后";
					}
					$(".neirong").append(html);

				}
			});

		}
	})

	function hb(id, status) {
		$.ajax({
			url: "{:url('agent/gold_close')}",
			data: {
				id: id,
				status: status
			},
			success: function(data) {
				if(data.code == 201) {
					layer.msg(data.msg, {
						icon: 1,
						time: 1000
					}, function() {
						window.location.href = "{:url('agent/hongb_list')}";
					});
				} else {
					layer.msg(data.msg, {
						icon: 1,
						time: 1000
					});
				}
			}

		})
	}
	//追加红包
	function gold_zj(gold_id) {
		$('#hb_id').val(gold_id);
		$('.tanchubox').show();
	}

	$('.tanc_btn').click(function() {
		if($('#user_num').val() > 500) {
			layer.msg('增加人数不能超过500');
			return;
		}
		if($('#gold_sum').val() > 1000) {
			layer.msg('增加鼓励金不能超过1000');
			return;
		}
		$.ajax({
			url: "{:url('agent/gold_zj')}",
			type: 'post',
			data: {
				id: $('#hb_id').val(),
				user_num: $('#user_num').val(),
				gold_sum: $('#gold_sum').val(),
				memo: $('#memo').val()
			},
			success: function(data) {
				if(data.code == 402) {
					layer.msg('商家鼓励金不足，无法创建');
					return false;
				}
				if(data.code == 403) {
					layer.msg('追加失败');
					return false;
				}

				layer.msg(data.msg, {
					icon: 1,
					time: 1000
				}, function() {
					window.location.href = "{:url('agent/hongb_list')}";
				});
			}
		})
	});

	function keyup(obj) {
		obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
		obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
		obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
		obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	}
</script>